﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Forums.OfflineStore;
using System.Threading;
using System.Windows.Threading;

namespace Microsoft.Forums.ForumsClient.Models
{
    public class PostViewModel : BindableObject, IEquatable<PostViewModel>
    {
        Post containedPost;

        /// <summary>
        /// The dispatcher from the UI thread
        /// </summary>
        private Dispatcher dispatcherUI;

        /// <summary>
        /// Post contained in this post container
        /// </summary>
        public Post Post
        {
            get
            {
                return containedPost;
            }
            private set
            {
                if (containedPost != value)
                {
                    containedPost = value;
                    OnPropertyChanged("Post");
                }
            }
        }


        /// <summary>
        /// Gets or sets the dispatcher UI.
        /// </summary>
        /// <value>The dispatcher UI.</value>
        protected Dispatcher DispatcherUI
        {

            get
            {
                return dispatcherUI;
            }
            set
            {
                dispatcherUI = value;
            }
        }

        /// <summary>
        /// Create a post view model for a given post
        /// </summary>
        /// <param name="post">Post to create container for</param>
        /// <param name="dispatcher">Dispatcher from the UI thread</param>
        public PostViewModel(Post post, Dispatcher dispatcher)
        {
            if (post == null) throw new ArgumentNullException("post", "post must not be null");
            if (dispatcher == null) throw new ArgumentNullException("dispatcher", "dispatcher must not be null");

            dispatcherUI = dispatcher;
            Post = ObjectRegistry.GetPost(post);
        }





        #region IEquatable<PostContainer> Members
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            PostViewModel pc = obj as PostViewModel;
            if (pc == null) return false;

            return Equals(pc);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public bool Equals(PostViewModel other)
        {
            if (other == null) return false;

            return other.Post.Id == this.Post.Id;
        }
        #endregion

        #region Command Handlers

        #region MarkAsRead
        /// <summary>
        /// Gets a value indicating whether this instance can mark as read.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance can mark as read; otherwise, <c>false</c>.
        /// </value>
        public bool CanMarkAsRead
        {
            get
            {
                return !Post.PostPropertySet.IsRead;
            }
        }
        /// <summary>
        /// Mark the post inside this post container as read.
        /// This modifies both the post object in memory and in the database
        /// </summary>
        public void MarkAsRead()
        {
            if (Post.PostPropertySet.IsRead) return;

            // First update local data
            Post.PostPropertySet.IsRead = true;
            var forum = ObjectRegistry.GetForum(Post.ForumId);
            forum.UnreadPostCount--;

            // Update database in background thread
            ThreadPool.QueueUserWorkItem
            (
                delegate
                {
                    // Update database
                    using (var store = AppDataModel.Instance.LocalDataStore)
                    {
                        var readPost = store.Posts.Single(p => p.Id == Post.Id);
                        readPost.PostPropertySet.IsRead = true;
                        store.SubmitChangesWithMerge();
                    }
                }


            );

        }

        #endregion

        #region MarkAsAnswer

        /// <summary>
        /// Gets a value indicating whether this instance can mark as answer.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance can mark as answer; otherwise, <c>false</c>.
        /// </value>
        public bool CanMarkAsAnswer
        {
            get
            {
                return !Post.IsAnswer;
            }
        }

        /// <summary>
        /// Marks as answer.
        /// </summary>
        public void MarkAsAnswer()
        {
            throw new NotImplementedException();
        }
        #endregion

        #endregion

    }
}
